import { SETUSERINFO,LOGOUT } from './actionTypes'
import {toLogin} from 'api'
import {message} from 'antd'
import {createHashHistory} from 'history'

const history =createHashHistory()

// 登录的异步操作
let toLoginAsync =(params,fromUrl) => {
  return (dispatch) =>{
    toLogin(params).then(res=>{
      if (res.data.code === '200') {
          message.success(res.data.msg,2,()=>{
            dispatch(setUserInfo(res.data.data.userInfo,res.data.data.token))
            history.push(fromUrl)
            history.go(0)
          })
      }
    })
  }
} 

// 设置存储用户信息
let setUserInfo = (userInfo,token) => {
  return {
      type: SETUSERINFO,
      userInfo,
      token
  }  
}

// 退出登录

let logOut = () => {
  return {
    type:LOGOUT
  }
}
export {
  toLoginAsync,
  logOut
}